<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>File Upload Example</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f4f4f4;
        }
        .upload-container {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            text-align: center;
        }
        input[type="file"] {
            margin: 10px 0;
        }
    </style>
</head>
<body>

<div class="upload-container">
    <h2>Upload a File</h2>
    <form id="uploadForm" enctype="multipart/form-data">
        <input type="file" id="fileInput" name="file" required>
        <br>
        <button type="submit">Upload</button>
    </form>
    <p id="status"></p>
</div>

<script>
    document.getElementById('uploadForm').addEventListener('submit', function(event) {
        event.preventDefault();
        const fileInput = document.getElementById('fileInput');
        const status = document.getElementById('status');

        if (fileInput.files.length === 0) {
            status.textContent = 'Please select a file.';
            return;
        }

        const formData = new FormData();
        formData.append('file', fileInput.files[0]);

        const xhr = new XMLHttpRequest();
        xhr.open('POST', '/upload', true);

        xhr.onload = function() {
            if (xhr.status === 200) {
                status.textContent = 'File uploaded successfully.';
            } else {
                status.textContent = 'File upload failed.';
            }
        };

        xhr.onerror = function() {
            status.textContent = 'An error occurred during the file upload.';
        };

        xhr.send(formData);
    });
</script>

</body>
</html>
